﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Common
{
    /// <summary>
    /// 文本文件帮助类
    /// <Remark></Remark>
    /// </summary>
    public class FileHelper
    {
        /// <summary>
        /// 将文本写入文本文件
        /// </summary>
        /// <param name="name">完整文件名</param>
        /// <param name="content">内容</param>
        /// <param name="isCover">是否覆盖存在文件</param>
        public static bool WriteToFile(string name, string content, bool isCover)
        {
            bool result;
            FileStream fs = null;
            try
            {
                if (!isCover && File.Exists(name))
                {
                    fs = new FileStream(name, FileMode.Append, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                    sw.WriteLine(content);
                    sw.Flush();
                    sw.Close();
                }
                else
                {
                    File.WriteAllText(name, content, Encoding.UTF8);
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
            return result;
        }


        /// <summary>
        /// 检查上传文件的扩展名
        /// </summary>
        /// <param name="_fileExt">扩展名</param>
        /// <returns>是否允许</returns>
        public static bool CheckFileExt(string _fileExt)
        {

            string[] allowExt = new string[] { ".gif", ".png", ".jpg", ".jpeg", ".bmp", ".swf", ".flv", ".wmv", ".rm", ".rmvb", ".3gp", ".avi", ".mp4", ".mpg", ".doc", ".xls", ".ppt", ".docx", ".xlsx", ".pptx", ".pdf", ".txt", ".rar", ".zip" };
            for (int i = 0; i < allowExt.Length; i++)
            {
                if (allowExt[i] == _fileExt.ToLower()) { return true; }
            }
            return false;

        }

        /// <summary>
        /// 删除目录及目录下所有文件
        /// </summary>
        /// <param name="_direcotryPath"></param>
        /// <returns></returns>
        public static bool DelDirectory(string _direcotryPath)
        {
            try
            {
                DirectoryInfo info = new DirectoryInfo(_direcotryPath);
                if (info != null && info.Exists)
                {
                    FileInfo[] files = info.GetFiles();
                    DirectoryInfo[] direcotries = info.GetDirectories();
                    if (files.Count() > 0)
                    {
                        int index = 0;
                        while (index < files.Count() - 1)
                        {
                            System.IO.File.Delete(files[index].FullName);
                        }
                    }

                    if (direcotries.Count() > 0)
                    {
                        int index = 0;
                        while (index < direcotries.Count() - 1)
                        {
                            DelDirectory(direcotries[index].FullName);
                        }
                    }
                    else
                    {
                        System.IO.Directory.Delete(_direcotryPath);
                    }
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        /// <summary>
        /// 文件重命名
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns>新名称</returns>
        public static string ReName(string fileName, string newName)
        {
            string newfileName = "";
            try
            {
                newfileName = newName + GetFileSuffix(fileName);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return newfileName;
        }


        /// <summary>
        /// 获取后缀
        /// </summary>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        public static string GetFileSuffix(string fileUrl)
        {           
            return Path.GetExtension(fileUrl); ;
        }

        ///// <summary>
        ///// 获取文件名         我问题当是/出错
        ///// </summary>
        ///// <param name="fileUrl"></param>
        ///// <returns></returns>
        //public static string GetFileName(string fileUrl)
        //{
        //    return fileUrl.Substring(fileUrl.LastIndexOf("\\")+1, fileUrl.Length - fileUrl.LastIndexOf("\\")-1);
        //}


        /// <summary>
        /// 复制文件（都是物理地址）
        /// </summary>
        /// <param name="source">原文件路径</param>
        /// <param name="destination">新文件路径</param>
        /// <returns></returns>
        public static bool CopyTo(string source, string destination)
        {
            try
            {
                System.IO.FileInfo file_s = new System.IO.FileInfo(source);
                System.IO.FileInfo file_d = new System.IO.FileInfo(destination);
                file_s.CopyTo(destination,true);

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return true;
        }

        /// <summary>
        /// 移动文件（都是物理地址）
        /// </summary>
        /// <param name="source">原文件路径</param>
        /// <param name="destination">新文件路径</param>
        /// <returns></returns>
        public static bool removeTo(string source, string destination)
        {
            try
            {
                System.IO.FileInfo file_s = new System.IO.FileInfo(source);
                System.IO.FileInfo file_d = new System.IO.FileInfo(destination);
                file_s.MoveTo(destination);

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return true;
        }


        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="?">文件的物理地址</param>
        /// <returns></returns>
        public static bool Delete(string fullFileUrl)
        {
            if (System.IO.File.Exists(fullFileUrl))
            {
                System.IO.File.Delete(fullFileUrl);
                return true;
            }
            return false;
        
        }

    }
}
